//
//  UserController.m
//  Happy to chat
//
//  Created by LL on 15/12/10.
//  Copyright © 2015年 LL. All rights reserved.
//

#import "UserController.h"
#import "PictureModel.h"
#import "shoucangViewCell.h"
@interface UserController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;
- (IBAction)backLoginAction:(id)sender;
- (IBAction)myVideo:(id)sender;
- (IBAction)myPicture:(id)sender;


@property (nonatomic, strong) NSMutableArray *array;
@end

@implementation UserController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.myTableView.delegate = self;
    
    self.myTableView.dataSource = self;
    
    self.myTableView.rowHeight = UITableViewAutomaticDimension;
    
    self.myTableView.estimatedRowHeight = 44;
    
    [self.myTableView registerNib:[UINib nibWithNibName:@"shoucangViewCell"bundle:nil] forCellReuseIdentifier:@"cell"];
    
//   [self.myTableView registerClass:[shoucangViewCell class] forCellReuseIdentifier:@"cell"];
//    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

}

#pragma mark --UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    shoucangViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
   PictureModel *model = self.array[indexPath.row];
    
    cell.model = model;
   
  
    return cell;
}


- (IBAction)backLoginAction:(id)sender {
    
    [AVUser logOut];
    [self.navigationController popViewControllerAnimated:YES];
}
int a = 0;
- (void)viewWillAppear:(BOOL)animated{
    a = 1;
    [self.array removeAllObjects];
    AVUser *current =  [AVUser currentUser];
    AVQuery *query = [AVQuery queryWithClassName:@"ImageModel"];
    [query whereKey:@"name" equalTo:current.username];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // 检索成功
           
            for (AVObject *obc in objects) {
                
                AVFile *file = [obc objectForKey:@"model"];
                
                NSData *data = [file getData];
                
                
                //创建反归档对象
                NSKeyedUnarchiver *nuarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
                //解码
                PictureModel *model = [nuarchiver decodeObjectForKey:@"secret"];
              
                //反归档完成
                [nuarchiver finishDecoding];
                
                
              
                [self.array insertObject:model atIndex:0];
                
                
                
            }
            [self.myTableView reloadData];
            
        } else {
            // 输出错误信息
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
    //    self.array = @[@"2",@"2",@"2",@"2",@"2",@"2"];


}
- (IBAction)myVideo:(id)sender {
   
    [self.array removeAllObjects];
   
    AVUser *current =  [AVUser currentUser];
    AVQuery *query = [AVQuery queryWithClassName:@"ImageModel"];
    [query whereKey:@"name" equalTo:current.username];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        
        if (!error) {
            // 检索成功
            NSLog(@"Successfully retrieved %lu posts.", (unsigned long)objects.count);
            for (AVObject *obc in objects) {
                
                AVFile *file = [obc objectForKey:@"model"];
                
                NSData *data = [file getData];
                NSLog(@"%@",[NSThread currentThread]);
               
                //创建反归档对象
                NSKeyedUnarchiver *nuarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
                //解码
                PictureModel *model = [nuarchiver decodeObjectForKey:@"secret"];
                NSLog(@"%@",model.wpic_small);
                //反归档完成
                [nuarchiver finishDecoding];
                
                [self.array insertObject:model atIndex:0];

            }
              [self.myTableView reloadData];
            
        } else {
            // 输出错误信息
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
      
    }];


}

- (IBAction)myPicture:(id)sender {
   
    

}
- (NSMutableArray *)array{
    
    if (_array == nil) {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
